package com.jiarong.repository.impl;

import com.jiarong.domain.Role;
import com.jiarong.repository.RoleDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author 加绒
 * @date 2021/10/15 16:38
 */
@Repository
public class RoleDaoImpl implements RoleDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<Role> selectAll() {
        return jdbcTemplate.query("select * from sys_role",
                new BeanPropertyRowMapper<>(Role.class));
    }

    @Override
    public void insertRole(Role role) {
        jdbcTemplate.update("insert into sys_role(id, roleName, roleDesc) values (?, ?, ?)",
                null, role.getRoleName(), role.getRoleDesc());
    }

    @Override
    public List<Role> selectRoleByUserId(Long id) {
        return jdbcTemplate.query("select * from sys_user_role ur, sys_role r " +
                        "where ur.roleId = r.id and ur.userId = ?",
                new BeanPropertyRowMapper<>(Role.class), id);
    }

    @Override
    public void deleteUserRoleRelationByRoleId(Long roleId) {
        jdbcTemplate.update("delete from sys_user_role where roleId = ?", roleId);
    }

    @Override
    public void deleteRole(Long roleId) {
        jdbcTemplate.update("delete from sys_role where id = ?", roleId);
    }
}
